Public Class FullScreen
    Dim F_Questions As New List(Of Question)
    Dim bClr1 As New Color
    Dim bClr2 As New Color
    Dim HasStart As Boolean = False
    Public Filename As String = ""

    Private Sub FullScreen_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
        If HasStart = True Then
            Exit Sub
        End If
        If e.KeyCode = Keys.Escape Then
            Cursor.Show()
            Form1.Show()
            Me.Close()
        End If
    End Sub
    Private Sub FullScreen_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Dim bmp As New Bitmap(Me.Width, Me.Height)
        Dim g As Graphics = Graphics.FromImage(bmp)
        g.FillRectangle(Brushes.Black, Me.Bounds)
        Cursor.Hide()
        g.DrawString("Click to begin Trivia Round or ESC. to exit round.", Me.Font, Brushes.White, (Me.Width / 2) - (g.MeasureString("Click to begin Trivia Round or ESC. to exit round.", Me.Font).Width / 2), 0)
        Me.BackgroundImage = bmp
    End Sub

    Public Sub LoadGame()
        On Error GoTo 150
        Dim Qs As New List(Of Question)
        If My.Computer.FileSystem.FileExists(Filename) = False Then
            Cursor.Show()
            MsgBox("The specified file at the specified location cannot be found!", MsgBoxStyle.Critical, "Error Loading File")
            Form1.Show()
            Me.Close()
        End If
        Dim Quest As New Question

        Dim lst As New List(Of String)

        lst = GetLoad(My.Computer.FileSystem.ReadAllText(Filename), "~")
        a = lst.Item(0)
50:     a = Mid(a, 3)
        'MsgBox(a)
        Dim Ans As New Answer
        Dim AnsLst As New List(Of String)
        comma1 = InStr(a, ";")
        'MsgBox("comma 1: " & comma1)
        q = Mid(a, 1, comma1 - 1)
        Quest.Question = q
        a = Mid(a, comma1 + 1)
        comma2 = InStr(a, ";")

        ID = Val(Mid(a, 1, comma2 - 1))

        a = Mid(a, comma2 + 1)
        Quest.ID = id 'Val(Mid(a, 1, 1))
        a = Mid(a, 3)

        Typ = Mid(a, comma2 + 1, 1)
        Quest.Type = Val(typ)

        '********Answers******************
        ansEnd = InStr(a, "}")
        Answer_ = Mid(a, 1, ansend - 1)

        correct = Mid(answer_, Len(answer_))
        Ans.CorrectIndex = correct
        'MsgBox("hello")
        answer_ = Mid(answer_, 1, Len(answer_) - 2)
        Do While Len(answer_) > 1
            div1 = InStr(answer_, "|")
            AnsLst.Add(Mid(answer_, 1, div1 - 1))
            'MsgBox(Mid(answer_, 1, div1 - 1))
            answer_ = Mid(answer_, div1 + 1)

        Loop
        Ans.Answers = AnsLst
        Ans.CorrectIndex = correct
        Quest.Answer = Ans
        Qs.Add(Quest)
        If Mid(a, ansend + 1, 1) = "{" Then
            a = Mid(a, ansend)
            GoTo 50
        End If

        'MsgBox("hello")

        b = lst.Item(1) 'background style
        If Mid(b, 1, 1) = "i" Then
            'image
            b = Mid(b, 3)
            b = Mid(b, 1, Len(b) - 1)
            Dim bmp As New Bitmap(Me.Width, Me.Height)
            bmp = Image.FromFile(b)
            Me.BackgroundImage = bmp
        ElseIf Mid(b, 1, 1) = "c" Then
            'color
            en = InStr(b, ">")
            clr = Mid(b, 3, en - 3)

            c1 = InStr(clr, ",")
            c2 = InStr(c1 + 1, clr, ",")
            Dim clr1 As New Color
            clr1 = Color.FromArgb(Mid(clr, 1, c1 - 1), Mid(clr, c1 + 1, c2 - c1 - 1), Mid(clr, c2 + 1, en - c2 - 2))
            Me.BackColor = clr1

        ElseIf Mid(b, 1, 1) = "g" Then
            'gradient
            en = InStr(b, ">")
            clr = Mid(b, 3, en - 2)
            'MsgBox(clr)
            c1 = InStr(clr, ",")
            c2 = InStr(c1 + 1, clr, ",")
            c3 = InStr(c2 + 1, clr, ",")
            c4 = InStr(c3 + 1, clr, ",")
            c5 = InStr(c4 + 1, clr, ",")
            c6 = InStr(c5 + 1, clr, ":")
            Dim clr1 As New Color
            Dim clr2 As New Color
            Dim angle As Integer
            clr1 = Color.FromArgb(Mid(clr, 1, c1 - 1), Mid(clr, c1 + 1, c2 - c1 - 1), Mid(clr, c2 + 1, c3 - c2 - 1))
            clr2 = Color.FromArgb(Mid(clr, c3 + 1, c4 - c3 - 1), Mid(clr, c4 + 1, c5 - c4 - 1), Mid(clr, c5 + 1, c6 - c5 - 1))
            angle = Val(Mid(clr, c6 + 1, Len(clr) - c6 - 1))
            'MsgBox(angle)
            Dim bmp As New Bitmap(Me.Width, Me.Height)
            Dim g As Graphics = Graphics.FromImage(bmp)
            Dim b1 As New Drawing2D.LinearGradientBrush(New Rectangle(0, 0, Me.Width, Me.Height), clr1, clr2, angle)
            g.FillRectangle(b1, New Rectangle(0, 0, Me.Width, Me.Height))
            Me.BackgroundImage = bmp
        End If
        'MsgBox("hello")
        c = lst.Item(2) 'question panel colors
        c = Mid(c, 2)
        c = Mid(c, 1, Len(c) - 1)
        c1 = InStr(c, ",")
        'MsgBox(c)
        c2 = InStr(c1 + 1, c, ",")
        c3 = InStr(c2 + 1, c, ",")
        c4 = InStr(c3 + 1, c, ",")
        c5 = InStr(c4 + 1, c, ",")
        Dim clrB As New Color
        Dim clrc As New Color
        clrB = Color.FromArgb(Mid(c, 1, c1 - 1), Mid(c, c1 + 1, c2 - c1 - 1), Mid(c, c2 + 1, c3 - c2 - 1))
        'MsgBox("hello")
        clrc = Color.FromArgb(Mid(c, c3 + 1, c4 - c3 - 1), Mid(c, c4 + 1, c5 - c4 - 1), Mid(c, c5 + 1))
        bClr1 = clrB
        bClr2 = clrc


        'MsgBox("hello")
        F_Questions = Qs
        randomi = lst.Item(3) 'randomize question order
        If LCase(randomi) = "false" Then
        Else 'F_Questions = RandList(F_Questions)
            Randomize(Now.Second)
            Dim l As New List(Of Question)
            Dim lst1 As List(Of Question) = F_Questions
            Dim r As New Random(Rnd(Now.Millisecond))
            Do While lst.Count > 0
                i = r.Next(1, lst.Count + 1)
                i = i - 1
                l.Add(lst1.Item(i))
                lst.RemoveAt(i)
            Loop
            F_Questions = l
        End If
        UpdateControls()
        Me.TopMost = True
        Me.Show()
        Exit Sub
150:    'error handling
        Cursor.Show()
        MsgBox("An error has occured while trying to load the saved game at the following location:" & (Chr(13)) & Filename & (Chr(13)) & "Error Description:" & (Chr(13)) & "Line:" & Err.Erl & (Chr(13)) & Err.Description, MsgBoxStyle.Critical, "Error")

        Form1.Show()
        Me.Close()
    End Sub

    Public Sub UpdateControls()
        Dim aa As New QuestionPanel
        Dim lst As New List(Of QuestionPanel)
        count = 0
        Dim Pnls As New List(Of QuestionPNL)
        Dim Pnl As New QuestionPNL
        Pnl.Visible = False
        Me.Controls.Add(Pnl)

        For Each q As Question In F_Questions
            Pnl.Visible = False
            Pnl.Question = q.Question
            Pnl.Answers = q.Answer.Answers
            Pnl.Correct = q.Answer.CorrectIndex

            Dim a As New QuestionPanel
            count = count + 1
            a.Name = "Question" & count
            'MsgBox(a.Name)
            a.Selected = False

            a.BackColor1 = bClr1
            a.BackColor2 = bClr2
            a.Question = q.ID
            a.Quest = q.Question
            a.Ans = q.Answer.Answers
            a.Correct = q.Answer.CorrectIndex

            lst.Add(a)

        Next
        For Each kk As QuestionPanel In lst
            'MsgBox(kk.Name)
        Next
        Dim w As Integer
        Dim h As Integer
        Dim width As Integer = aa.Width + 10
        Dim height As Integer = aa.Height + 10
        w = Math.Sqrt(lst.Count)
        'MsgBox("w = " & w)
        '** to here is OK
        If w * width > Me.Width Then
            w = w - 1

        End If
        h = lst.Count / w

        'MsgBox(width & " " & height)
        wide = ((width * w) - 10) / 2
        high = ((height * h) - 10) / 2
        'MsgBox(wide & " " & high)
        xpos1 = Me.Width / 2 - wide
        ypos1 = Me.Height / 2 - high
        ypos = ypos1 'xpos1 - wide
        xpos = xpos1
        'MsgBox(lst.Count)
        Dim ctls As New List(Of Control)
        Dim Ctl As New QuestionPanel
        index = -1


        For y = 1 To h + 10
            For x = 1 To w
                index += 1
                If index >= lst.Count Then
                    GoTo 75
                End If
                'MsgBox(index & " out of " & lst.Count - 1)
                Ctl = lst.Item(index)
                Ctl.Location = New Point(xpos + ((x - 1) * width), ypos + ((y - 1) * height))
                Ctl.Visible = True
                Ctl.BringToFront()
                Ctl.Panel = Pnl
                Me.Controls.Add(Ctl)
                Ctl = Nothing
            Next
        Next
75:
        Exit Sub
        If h * w < lst.Count Then
            MsgBox("")
            width2 = (lst.Count - (h * w)) * width
            height2 = height
            xpos2 = (Me.Width / 2) - (width2 / 2)
            ypos2 = ypos + (y * height)
            For y = (h * w) + 1 To lst.Count
                Ctl = lst.Item(y - 1)
                Ctl.Location = New Point(xpos2 + ((h - 1) * width), ypos2)
                Ctl.Visible = True
                Ctl.BringToFront()
                Ctl.Panel = Pnl
                Me.Controls.Add(Ctl)
                Ctl = Nothing
            Next
        End If
        ' '' ''For x = 1 To w
        ' '' ''    xpos += wide
        ' '' ''    For y = 1 To h


        ' '' ''        index = y + x - 1

        ' '' ''        Ctl = lst.Item(index - 1)
        ' '' ''        'MsgBox(index & " " & Ctl.Name)
        ' '' ''        Ctl.Location = New Point(xpos, ypos)
        ' '' ''        Ctl.Visible = True
        ' '' ''        Ctl.BringToFront()

        ' '' ''        ypos += height
        ' '' ''        Me.Controls.Add(Ctl)
        ' '' ''        'MsgBox(Ctl.Name)
        ' '' ''        Ctl = Nothing
        ' '' ''    Next

        ' '' ''    ypos = ypos1
        ' '' ''Next

    End Sub

    Private Sub FullScreen_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseClick
        If HasStart = False Then
            ExitButton1.Visible = True
            Me.BackgroundImage = Nothing
            LoadGame()

            Cursor.Show()
            ExitButton1.Redraw()
        End If
    End Sub

    Private Sub ExitButton1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ExitButton1.MouseClick
        Form1.Show()
        Me.Close()
    End Sub

End Class